//
//  FeedbackViewController.swift
//  PDF2PNG
//
//  Created by Jarvis on 12/05/2017.
//  Copyright © 2017 Jarvis. All rights reserved.
//

import UIKit

class FeedbackViewController: ViewController {

    var postButton: UIButton!
    var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Feedback".localized()
        view.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0)
        
        prepareUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func prepareUI() {
        
        postButton = UIButton()
        view.addSubview(postButton)
        postButton.setTitle("Post".localized(), for: .normal)
        postButton.setTitleColor(UIColor.white, for: .normal)
        postButton.clipsToBounds = true
        postButton.layer.cornerRadius = 6
        postButton.backgroundColor = UIColor(red: 0, green: 0.5, blue: 1, alpha: 1)
        postButton.addTarget(self, action: #selector(postButtonPressed(_:)), for: .touchUpInside)
        postButton.snp.makeConstraints { (make) in
            make.height.equalTo(50)
            make.width.equalTo(240)
            make.top.equalTo(view.snp.top).offset(220)
            make.centerX.equalToSuperview()
//            make.center.equalToSuperview()
        }
        
        textView = UITextView()
        view.addSubview(textView)
        textView.backgroundColor = UIColor.white
        textView.clipsToBounds = true
        textView.layer.cornerRadius = 6
        textView.font = UIFont.systemFont(ofSize: 16)
        textView.textColor = UIColor.darkGray
        textView.snp.makeConstraints { (make) in
//            make.height.equalTo(120)
            make.top.equalTo(view.snp.top).offset(80)
            make.bottom.equalTo(postButton.snp.top).offset(-20)
            make.left.equalTo(view.snp.left).offset(10)
            make.right.equalTo(view.snp.right).offset(-10)
        }
        
        textView.text = "Please enter your comments and suggestions here."
        let range = NSRange(location: 0, length: textView.text.characters.count)
        textView.selectedRange = range
    }
    
    @objc func postButtonPressed(_ button: UIButton) {
        
        if let message = textView.text {
            if message == "Please enter your comments and suggestions here." {
                Toast.showMessage(message: "Please enter something in the textview.")
                return
            }
            
            if message.characters.count > 15 {
                if message.characters.count < 256 {
                    Tracker.feedback(message: message)
                    
                    navigationController?.popViewController(animated: true)
                    
                    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                        Toast.showMessage(message: "Feedback sent successfully.")
                    }
                }else {
                    Toast.showMessage(message: "Please enter less than 256 characters.")
                }
            }else {
                Toast.showMessage(message: "Please enter no less than 16 characters.")
            }
        }
        
        
    }

}
